package com.hsc.yalebao.base;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.os.Process;
import android.util.DisplayMetrics;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.Volley;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.hsc.yalebao.model.UserBaseBean;
import com.hsc.yalebao.tools.DomainToIPTask;
import com.hsc.yalebao.tools.SharedPreferencesUtil;
import com.hsc.yalebao.tools.UiUtil;
import com.hsc.yalebao.weight.LogUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;

public class CustomApplication extends Application {
    public static Stack<Activity> activityStack;
    public static CustomApplication app = null;
    public static String ipStr = "";
    public String IMEI = "";
    public String NetIP = "nQlWNNpPZ5OiVs107j6zmw%3D%3D";
    private String TAG = "CustomApplication";
    public String apkName = "";
    public String checkLog = "";
    public Context context;
    public DisplayMetrics displayMetrics;
    public boolean isLogin = false;
    public boolean isNetConnect = false;
    private RequestQueue mRequestQueue;
    public String openId = "";
    Handler parseDomainToIPHandler = new Handler() {
        public void handleMessage(Message paramAnonymousMessage) {
            if (paramAnonymousMessage.what == -1)
                CustomApplication.ipStr = (String) paramAnonymousMessage.obj;
        }
    };
    public SharedPreferencesUtil preferencesUtil;
    public double userAmount = 0.0D;
    public UserBaseBean userBaseBean;
    public String userPhone = "";
    public String weChatHeadImgFileName = "";
    public String weChatNickName = "";


    public static CustomApplication getInstance() {
        return app;
    }

    public static CustomApplication getIntance() {
        if (app == null)
            app = new CustomApplication();
        return app;
    }

    private void getTelephoneInfo() {
        app.IMEI = Build.FINGERPRINT;
    }

    private void initFresco() {
        Fresco.initialize(this.context);
    }

    public void addActivity(Activity paramActivity) {
        if (activityStack == null)
            activityStack = new Stack();
        activityStack.add(paramActivity);
    }


    public Activity currentActivity() {
        return (Activity) activityStack.lastElement();
    }

    public void finishActivity(Activity paramActivity) {
        if (paramActivity != null) {
            activityStack.remove(paramActivity);
            paramActivity.finish();
        }
    }

    public void finishActivity(Class<?> paramClass) {
        Iterator localIterator = activityStack.iterator();
        while (true) {
            if (!localIterator.hasNext())
                return;
            Activity localActivity = (Activity) localIterator.next();
            if (localActivity.getClass().equals(paramClass))
                finishActivity(localActivity);
        }
    }

    public void finishAllActivity() {
        int i;
        if (activityStack != null) {
            i = 0;
            if (i >= activityStack.size())
                activityStack.clear();
        } else {
            return;
        }
        Activity localActivity = (Activity) activityStack.get(i);
        if (localActivity == null) ;
        while (true) {
            i++;
            localActivity.finish();
            break;
        }
    }


    public Activity getActivityByName(String paramString) {
        try {
            Iterator localIterator = activityStack.iterator();
            Activity localActivity;
            boolean bool;
            do {
                if (!localIterator.hasNext())
                    return null;
                localActivity = (Activity) localIterator.next();
                bool = localActivity.getClass().getSimpleName().equals(paramString);
            }
            while (!bool);
            return localActivity;
        } catch (Exception localException) {
        }
        return null;
    }


    public RequestQueue getRequestQueue() {
        if (this.mRequestQueue == null)
            this.mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        return this.mRequestQueue;
    }

    public DisplayMetrics getScreenSize() {
        this.displayMetrics = getResources().getDisplayMetrics();
        System.out.println("当前设备屏幕高度（像素）:" + this.displayMetrics.heightPixels);
        System.out.println("当前设备屏幕宽度（像素）:" + this.displayMetrics.widthPixels);
        return this.displayMetrics;
    }

    public String getVersionName() {
        PackageManager localPackageManager = getPackageManager();
        try {
            String str = localPackageManager.getPackageInfo(getPackageName(), 0).versionName;
            return str;
        } catch (PackageManager.NameNotFoundException localNameNotFoundException) {
            localNameNotFoundException.printStackTrace();
        }
        return "";
    }

    public void initImgLoader() {
        DisplayImageOptions localDisplayImageOptions = new DisplayImageOptions.Builder().showImageForEmptyUri(0).showImageOnFail(0).cacheInMemory(true).cacheOnDisc(true).build();
        ImageLoaderConfiguration localImageLoaderConfiguration = new ImageLoaderConfiguration.Builder(getApplicationContext()).defaultDisplayImageOptions(localDisplayImageOptions).discCacheSize(52428800).discCacheFileCount(100).writeDebugLogs().build();
        ImageLoader.getInstance().init(localImageLoaderConfiguration);
    }

    public void onCreate() {
        super.onCreate();
        this.context = this;
        app = this;
        DomainToIPTask localDomainToIPTask = new DomainToIPTask(this.parseDomainToIPHandler);
        String[] arrayOfString = new String[1];
//        arrayOfString[0] = AppConstants.IP;
        localDomainToIPTask.execute(arrayOfString);
        this.preferencesUtil = SharedPreferencesUtil.getInstance(this);
        getTelephoneInfo();
        getScreenSize();
        initImgLoader();
        initFresco();
        CrashHandler.getInstance().init(getApplicationContext());
    }

    public void onTerminate() {
        super.onTerminate();
    }

    public void showTimeoutMsg(String paramString) {
        UiUtil.showToast(getApplicationContext(), paramString);
    }
}